package com.example.mall.service.impl;

import com.example.mall.mapper.SpOrderGoodsMapper;
import com.example.mall.mapper.SpOrderMapper;
import com.example.mall.pojo.OrderUpdateData;
import com.example.mall.pojo.SpOrder;
import com.example.mall.pojo.SpOrderGoods;
import com.example.mall.service.SpOrderService;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
@AllArgsConstructor
public class SpOrderServiceImpl implements SpOrderService {

    SpOrderMapper spOrderMapper;
    SpOrderGoodsMapper spOrderGoodsMapper;

    @Override
    public List<SpOrder> getSpOrderByQuery(@RequestParam("query") String query,@RequestParam("pagenum") Integer pagenum, @RequestParam("pagesize") Integer pagesize) {
        if(query == "") {
            PageHelper.startPage(pagenum,pagesize);
//        System.out.println("分页pagenum"+pagenum);
//        System.out.println("分页pagesize"+pagesize);
            List<SpOrder> so = spOrderMapper.selectAll();
            return so;
        }else{
            PageHelper.startPage(pagenum,pagesize);
            List<SpOrder> so = spOrderMapper.selectAllByQuery(query);
            return so;
        }
    }

//    @Override
//    public List<SpOrder> getSpOrderById(Integer order_id) {
//        List<SpOrder> spOrders=spOrderMapper.selectOrderById(order_id);
//        return spOrders;
//    }

    @Override
    public SpOrder getSpOrderById(Integer order_id) {
//        SpOrder spOrder = new SpOrder();
        SpOrder sp=spOrderMapper.selectById(order_id);
        return sp;
    }



    @Override
    public OrderUpdateData updateOrder(SpOrder spOrder){
        int i =spOrderMapper.updateById(spOrder);

        //响应数据
        OrderUpdateData orderUpdateData = new OrderUpdateData();
        if(i>0){
            //更新成功
            SpOrder sp = spOrderMapper.selectById(spOrder.getOrder_id());
            if(sp.getOrder_id()!=null)
                orderUpdateData.setOrder_id(sp.getOrder_id());
            if(sp.getUser_id()!=null)
                orderUpdateData.setUser_id(sp.getUser_id());
            if(sp.getOrder_number()!=null)
                orderUpdateData.setOrder_number(sp.getOrder_number());
            if(sp.getOrder_price()!=null)
                orderUpdateData.setOrder_price(sp.getOrder_price());
            if(sp.getOrder_pay()!=null)
                orderUpdateData.setOrder_pay(sp.getOrder_pay());
            if(sp.getIs_send()!=null)
                orderUpdateData.setIs_send(sp.getIs_send());
            if(sp.getTrade_no()!=null)
                orderUpdateData.setTrade_no(sp.getTrade_no());
            if(sp.getOrder_fapiao_title()!=null)
                orderUpdateData.setOrder_fapiao_title(sp.getOrder_fapiao_title());
            if(sp.getOrder_fapiao_company()!=null)
                orderUpdateData.setOrder_fapiao_company(sp.getOrder_fapiao_company());
            if(sp.getOrder_fapiao_content()!=null)
                orderUpdateData.setOrder_fapiao_content(sp.getOrder_fapiao_content());
            if(sp.getConsignee_addr()!=null)
                orderUpdateData.setConsignee_addr(sp.getConsignee_addr());
            if(sp.getPay_status()!=null)
                orderUpdateData.setPay_status(sp.getPay_status());
            if(sp.getCreate_time()!=null)
                orderUpdateData.setCreate_time(sp.getCreate_time());
            if(sp.getUpdate_time()!=null)
                orderUpdateData.setUpdate_time(sp.getUpdate_time());
            SpOrderGoods spOrderGoods = new SpOrderGoods();
//            SpOrderGoods.Criteria spOrderGoodscriteria = spOrderGoods.createCriteria();
//            spOrderGoodscriteria.andOrderIdEqualTo(sp.getOrder_id());
            List<SpOrderGoods> SOG = spOrderGoodsMapper.selectSpOrderGoods(sp.getOrder_id());
            orderUpdateData.setGoods(SOG);
        }
        return orderUpdateData;
    }



}

